package com.image_see;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.bumptech.glide.Glide;
import com.mylibrary.R;
import com.touch_imageview.ExtendedViewPager;
import com.touch_imageview.TouchImageView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by x_luthy on 2016/4/12.
 */
public class ImageSeeActivity
        extends
        Activity
{
	private ExtendedViewPager viewPager;
	private HolderView		  holder;
	private Context			  context;
	private List<String>	  urlList = new ArrayList<>();
									  
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//取消状态栏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_image_see);
		
		context = this;
		findView();
		if (!getIntent().getExtras().containsKey("urlList"))
		{
			finish();
			return;
		}
		urlList = getIntent().getStringArrayListExtra("urlList");
		viewPager.setAdapter(new VpAdapter());
		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
		{
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
			
			}
			
			@Override
			public void onPageSelected(int position) {
			
			}
			
			@Override
			public void onPageScrollStateChanged(int state) {
			
			}
		});
	}
	
	private void findView() {
		viewPager = (ExtendedViewPager) findViewById(R.id.viewpager);
		viewPager.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	private class VpAdapter
	        extends
	        PagerAdapter
	{
		
		@Override
		public int getCount() {
			return urlList.size();
		}
		
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View view = null;
			view = LayoutInflater.from(context).inflate(R.layout.image_see_item, viewPager, false);
			holder = new HolderView();
			holder.ivItem = (TouchImageView) view.findViewById(R.id.iv_item);
			holder.ivItem.setOnClickListener(new View.OnClickListener()
			{
				@Override
				public void onClick(View v) {
					finish();
				}
			});
			if (urlList.get(position).length() >= 4 && urlList.get(position).substring(0, 4).equals("http"))
			{
				Glide.with(context).load(urlList.get(position)).placeholder(R.drawable.ima_default_img).into(holder.ivItem);
			}
			else
			{
//				Glide.with(context).load(new File(urlList.get(position))).into(holder.ivItem);
				Bitmap bitmap = compressImageFromFile(urlList.get(position));
				holder.ivItem.setImageBitmap(bitmap);
			}
			container.addView(view);
			return view;
		}
		
		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}
		
	}
	
	private class HolderView
	{
		TouchImageView ivItem;
	}

	/**
	 * 压缩图片
	 *
	 * @param srcPath
	 * @return
	 */
	private Bitmap compressImageFromFile(String srcPath) {
		BitmapFactory.Options newOpts = new BitmapFactory.Options();
		newOpts.inJustDecodeBounds = true;//只读边,不读内容
		Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);

		newOpts.inJustDecodeBounds = false;
		int w = newOpts.outWidth;
		int h = newOpts.outHeight;
		float hh = 800f;//
		float ww = 480f;//
		int be = 1;
		if (w > h && w > ww)
		{
			be = (int) (newOpts.outWidth / ww);
		}
		else if (w < h && h > hh)
		{
			be = (int) (newOpts.outHeight / hh);
		}
		if (be <= 0)
			be = 1;
		newOpts.inSampleSize = be;//设置采样率

		newOpts.inPreferredConfig = Bitmap.Config.ARGB_8888;//该模式是默认的,可不设
		newOpts.inPurgeable = true;// 同时设置才会有效
		newOpts.inInputShareable = true;//。当系统内存不够时候图片自动被回收

		bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
		return bitmap;
	}
	
}
